package com.heer.gymnasium.module.fellow.views;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.evaluate.views.MemberListActivity;
import com.heer.gymnasium.module.fellow.beans.MatchBean;
import com.heer.gymnasium.module.fellow.beans.MatchResponseBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.order.beans.OrderResponseBean;
import com.heer.gymnasium.module.order.views.OrderActivity;
import com.heer.gymnasium.module.order.views.PaidSuccessActivity;
import com.heer.gymnasium.module.reservation.beans.ReservationBookItemResponseBean;
import com.heer.gymnasium.module.reservation.beans.ReservationGridBean;
import com.heer.gymnasium.module.team.views.TeamDetailActivity;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.gymnasium.util.StringUtils;
import com.heer.gymnasium.util.WxShareUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 拼单详情
 * Created by zy on 2018/2/24.
 */

public class FellowDetailActivity extends BaseActivity {
    private final static int RES_LAYOUT_ROOT = R.layout.layout_fellow_detail;
    private final static int RES_ID_FELLOW_PANEL = R.id.layout_fellow_detail_fellow_panel_ll;
    private final static int RES_ID_MATCH_PANEL = R.id.layout_fellow_detail_match_panel_ll;

    private final static int RES_ID_TITLE_RIGHT = R.id.layout_title_bar_left_iv;

    private final static int RES_ID_JOIN_MEMBER = R.id.layout_fellow_detail_joiners_ll;

    private final static int RES_ID_TEAM_DETAIL = R.id.layout_fellow_detail_team_detail_ll;
    private final static int RES_ID_MATCH_ICON = R.id.layout_fellow_detail_match_icon_iv;
    private final static int RES_ID_MATCH_NAME = R.id.layout_fellow_detail_match_name_tv;
    private final static int RES_ID_MATCH_IPONE = R.id.layout_fellow_detail_match_ipone_tv;

    private final static int RES_ID_DATE = R.id.layout_fellow_detail_date_tv;
    private final static int RES_ID_ADDRESS = R.id.layout_fellow_detail_address_tv;
    private final static int RES_ID_CONTENT = R.id.layout_fellow_detail_memo_tv;
    private final static int RES_ID_JOIN = R.id.layout_fellow_detail_join_tv;

    private final static int RES_ID_CATEGORY = R.id.layout_fellow_detail_category_iv;
    private final static int RES_ID_FIELD = R.id.layout_fellow_detail_field_tv;
    private final static int RES_ID_PRICE = R.id.layout_fellow_detail_price_tv;
    private final static int RES_ID_FELLOW_COUNT = R.id.layout_fellow_detail_fellow_count_tv;

    private final static int DRAWABLE_DEFAULT_CATEGORY = R.drawable.ic_hobby_glof;
    private final static int DRAWABLE_SHARE = R.drawable.ic_share;
    @BindView(RES_ID_CATEGORY)
    ImageView ivCategory;
    @BindView(RES_ID_DATE)
    TextView tvDate;
    @BindView(RES_ID_FIELD)
    TextView tvField;
    @BindView(RES_ID_PRICE)
    TextView tvPrice;
    @BindView(RES_ID_ADDRESS)
    TextView tvAddress;
    @BindView(RES_ID_FELLOW_COUNT)
    TextView tvFellowCount;
    @BindView(RES_ID_CONTENT)
    TextView tvContent;

    @BindView(RES_ID_MATCH_ICON)
    ImageView ivMatchIcon;
    @BindView(RES_ID_MATCH_NAME)
    TextView tvMatchName;
    @BindView(RES_ID_MATCH_IPONE)
    TextView tvMatchIphone;
    @BindView(RES_ID_JOIN)
    TextView tvJoin;

    @BindView(RES_ID_FELLOW_PANEL)
    View fellowView;
    @BindView(RES_ID_MATCH_PANEL)
    View matchView;

    private FellowDetailActivity $this;


    private int type;
    private String orderId;
    private MatchBean.Info bean;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_LAYOUT_ROOT);
        type = getIntent().getIntExtra(EXTRA_TYPE, Type.TYPE_FELLOW);
        orderId = getIntent().getStringExtra(EXTRA_ID);
    }


    @Override
    protected void initView() {
        super.initView();
        type = getIntent().getIntExtra(EXTRA_TYPE, 0);
        orderId = getIntent().getStringExtra(EXTRA_ID);

        titleUtils.setRightImage(DRAWABLE_SHARE, 0);
        titleUtils.setRightOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(orderId)) {
                    ToastUtil.show($this, "信息异常");
                    return;
                }
                WxShareUtils wxShareUtils = new WxShareUtils($this);
                if (type == Type.TYPE_FELLOW) {
                    wxShareUtils.wxShare(orderId, Type.SHARE_FELLOW);
                } else {
                    wxShareUtils.wxShare(orderId, Type.SHARE_MATCH);
                }
            }
        });

        if (type == Type.TYPE_FELLOW) {
            fellowView.setVisibility(View.VISIBLE);
            matchView.setVisibility(View.GONE);
            tvJoin.setText("参与拼单");
            titleUtils.setTitleText("拼单详情");
        } else if (type == Type.TYPE_MATCH) {
            matchView.setVisibility(View.VISIBLE);
            fellowView.setVisibility(View.GONE);
            tvJoin.setText("应战");
            titleUtils.setTitleText("约战详情");
        } else {
            titleUtils.setTitleText("约战详情");
            type = Type.TYPE_MATCH;
            matchView.setVisibility(View.VISIBLE);
            fellowView.setVisibility(View.GONE);
            tvJoin.setText("报名");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        getMatchInfo();
    }

    @OnClick(RES_ID_JOIN_MEMBER)
    public void toSeeMember() {
        if (!TextUtils.isEmpty(orderId)) {
            FellowMemberActivity.startActivity($this, orderId);
        } else {
            ToastUtil.show($this, "订单ID为空");
        }
    }


    private void getMatchInfo() {
        mAppHelper.apiService().getYPActivityInfo(orderId).enqueue(new BaseCallback<ApiResponse<MatchResponseBean.Info>>() {
            @Override
            public void onSuccess(Call<ApiResponse<MatchResponseBean.Info>> call, Response<ApiResponse<MatchResponseBean.Info>> response) {
                bean = MatchBean.dataChane(response.body().getData());
                tvDate.setText(bean.getTime_day());
                ImageUtil.loadUrl($this, "", DRAWABLE_DEFAULT_CATEGORY, ivCategory);
                String field = "";
                for (int i = 0, max = bean.getPlace().size(); i < max; i++) {
                    field += bean.getPlace().get(i) + "\t";
                    field += bean.getTime().get(i) + (i < max - 1 ? "\r\n" : "");
                }
                tvField.setText(field);
                tvPrice.setText(String.format("%s元", bean.getPrice()));
                tvAddress.setText(bean.getAddress());
                tvContent.setText(bean.getInfo());
                if (type == Type.TYPE_MATCH) {
                    ImageUtil.loadUrl($this, bean.getCorps_logo(), DRAWABLE_DEFAULT_CATEGORY, ivMatchIcon);
                    tvMatchName.setText(bean.getCorps_name());
                } else if (type == Type.TYPE_FELLOW) {
                    tvFellowCount.setText(bean.getOrder_num_a() + "/" + bean.getOrder_num() + "人");
                }
            }

            @Override
            public void onError(Call<ApiResponse<MatchResponseBean.Info>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });
    }

    @OnClick(RES_ID_TEAM_DETAIL)
    public void toSeeTeamDetail() {
        if (bean != null) {
            if (type == Type.TYPE_MATCH) {
                TeamDetailActivity.startActivity($this, bean.getCorps_id());
            }
        }
    }

    private final static int REQUEST_CODE_MATCH = 0x7;
    private final static int RRESULT_CODE_MATCH = 0x8;

    @OnClick(RES_ID_JOIN)
    public void toJoin() {
        if (!TextUtils.isEmpty(orderId)) {
            int matchType = getIntent().getIntExtra(EXTRA_TYPE, 0);
            if (matchType == Type.TYPE_MESSAGE) {
                enrollMatchActivity(orderId);
            } else if (type == Type.TYPE_MATCH) {
                InitiateMatchActivity.startActivity($this, REQUEST_CODE_MATCH, StringUtils.toFloat(bean.getPrice()), "Match");
            } else if (type == Type.TYPE_FELLOW) {
                addOrder(orderId, Type.ORDER_FELLOW, "");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_MATCH) {
            if (resultCode == RRESULT_CODE_MATCH) {
                String tid = data.getStringExtra(EXTRA_ID);
                addOrder(bean.getOrder_id(), Type.ORDER_YUE, tid);
            }
        }
    }

    private void addOrder(String order_id, String order_type, String corps_id) {
        List<ReservationGridBean> newList = new ArrayList<>();
        String
                place_id = null,
                start_time = null,
                end_time = null,
                plan_info = null,
                card_id = null,
                coach_id = null,
                num = null,
                category_id = null,
                order_num = null,
                info = null;
        mAppHelper.apiService().addOrder(order_type,
                place_id,
                start_time,
                end_time,
                plan_info,
                card_id,
                coach_id,
                num,
                corps_id,
                order_id,
                order_num,
                info, category_id,"").enqueue(new BaseCallback<ApiResponse<OrderResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<OrderResponseBean>> call, Response<ApiResponse<OrderResponseBean>> response) {
                OrderActivity.startActivity($this, OrderActivity.OrderType.from_coupon, response.body().getData().getOrder_id(), "");
            }
        });
    }

    // 战队成员活动报名
    private void enrollMatchActivity(String orderId) {
        mAppHelper.apiService().enrollMatchActivity(orderId).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                PaidSuccessActivity.startActivity($this, 1);
                ToastUtil.show($this, "报名成功！");
            }
        });
    }

    public static void startActivity(Context context, int type, String id) {
        Intent mIntent = new Intent(context, FellowDetailActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        mIntent.putExtra(EXTRA_TYPE, type);
        context.startActivity(mIntent);
    }


}
